package com.springboot.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service

public class SendMailService {
    @Autowired
    private JavaMailSenderImpl mailSender;

    public void sendHtmlMail(String from, String to, String content) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setSubject("Your password is：");
        // 启用html
        mimeMessageHelper.setText(content, true);
        // 发送邮件
        mailSender.send(mimeMessage);
        System.out.println("邮件已发送");
    }

    //将过期书籍发送到读者邮箱
    public void sendHtmlMailBook(String from, String to, String content) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setSubject("You have kept the books for more than 30 days, please return it promptly:");
        // 启用html
        mimeMessageHelper.setText(content, true);
        // 发送邮件
        mailSender.send(mimeMessage);
        System.out.println("邮件已发送");
    }
}
